﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Customer.Core.ServiceCommand.CustomerManager
{
    public class RetrieveCustomerAlertBadBankServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerAlertRequest, RetrieveCustomerAlertBadbankResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int SearchIndex;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetrieveCustomerAlertBadBankServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerAlertRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            SearchIndex = request.Type;
        }

        protected override bool OnExecute(RetrieveCustomerAlertBadbankResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<AlertCustomerRecord> records = new List<AlertCustomerRecord>();
            switch (SearchIndex)
            {
                case 1:
                    records = GetAlertBadbankTopdown();
                    break;
                case 2:
                    records = GetAlertBadbankWatchlist();
                    break;
                case 3:
                    records = GetAlertBadbankBottomup();
                    break;
                default:
                    break;
            }
            response.Records = records;
            response.Page = new PageRecord
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalRecords = totalRecord
            };
            return isSuccess;
        }
        private List<AlertCustomerRecord> GetAlertBadbankTopdown()
        {
            List<AlertCustomerRecord> records = new List<AlertCustomerRecord>();
            List<AlertBadbankTopdown> listCustomer = new List<AlertBadbankTopdown>();
            Dictionary<string, Branch> listBranches = Providers.Branchs.RetrieveAllBranches().ToDictionary(x => x.BranchId);
            //string YearMonth = DateTime.Now.Year.ToString() +  DateTime.Now.Month.ToString();
            listCustomer = Providers.CustomerAlerts.RetrieveCustomerAlertBadBankTopdown(out totalRecord, Dao, Request.Lang, TextSearch, pageSize, pageIndex, true);
            if (listCustomer != null)
            {
                records = listCustomer.Select(x => new AlertCustomerRecord
                {
                    Zone = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].Region : string.Empty,
                    DaoName = x.DaoName,
                    Dao = x.Dao,
                    Branch = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].BranchNameSme : string.Empty,
                    Cif = x.Cif,
                    CustName = x.CusName,
                    Industry = x.IndustryName,
                    IndustryEn = x.IndustryNameEn,
                    Month = string.IsNullOrEmpty(x.Month) ? string.Empty : x.Month,
                    Month1 = string.IsNullOrEmpty(x.Month1) ? string.Empty : x.Month1,
                    Month2 = string.IsNullOrEmpty(x.Month2) ? string.Empty : x.Month2
                }).ToList();
            }
            return records;
        }
        private List<AlertCustomerRecord> GetAlertBadbankBottomup()
        {
            List<AlertCustomerRecord> records = new List<AlertCustomerRecord>();
            List<AlertBadbankBottomup> listCustomer = new List<AlertBadbankBottomup>();
            Dictionary<string, Branch> listBranches = Providers.Branchs.RetrieveAllBranches().ToDictionary(x => x.BranchId);
            //string YearMonth = DateTime.Now.Year.ToString() +  DateTime.Now.Month.ToString();
            listCustomer = Providers.CustomerAlerts.RetrieveCustomerAlertBadBankBottomup(out totalRecord, Dao, Request.Lang, TextSearch, pageSize, pageIndex, true);
            if (listCustomer != null)
            {
                records = listCustomer.Select(x => new AlertCustomerRecord
                {
                    Zone = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].Region : string.Empty,
                    DaoName = x.DaoName,
                    Dao = x.Dao,
                    Branch = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].BranchNameSme : string.Empty,
                    Cif = x.Cif,
                    CustName = x.CusName,
                    Industry = x.IndustryName,
                    IndustryEn = x.IndustryNameEn,
                    Color = x.Color
                }).ToList();
            }
            return records;
        }
        private List<AlertCustomerRecord> GetAlertBadbankWatchlist()
        {

            List<AlertCustomerRecord> records = new List<AlertCustomerRecord>();
            List<AlertBadbankWatchlist> listCustomer = new List<AlertBadbankWatchlist>();
            Dictionary<string, Branch> listBranches = Providers.Branchs.RetrieveAllBranches().ToDictionary(x => x.BranchId);
            //string YearMonth = DateTime.Now.Year.ToString() +  DateTime.Now.Month.ToString();
            listCustomer = Providers.CustomerAlerts.RetrieveCustomerAlertBadBankWatchlist(out totalRecord, Dao, Request.Lang, TextSearch, pageSize, pageIndex, true);
            if (listCustomer != null)
            {
                records = listCustomer.Select(x => new AlertCustomerRecord
                {
                    Zone = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].Region : string.Empty,
                    DaoName = x.DaoName,
                    Dao = x.Dao,
                    Branch = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].BranchNameSme : string.Empty,
                    Cif = x.Cif,
                    CustName = x.CusName,
                    Industry = x.IndustryName,
                    IndustryEn = x.IndustryNameEn
                }).ToList();
            }
            return records;
        }
    }
}
